package com.zookeeper.te3;

import com.alibaba.fastjson.JSON;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 * 作者: LCG
 * 日期: 2020/2/26 22:14
 * 描述: Zookeeper客户端的使用
 */
public class Test1704Zookeeper {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        Test04Watcher test04Watcher = new Test04Watcher();

        ZooKeeper zooKeeper=new ZooKeeper("127.0.0.1:2181",5000,test04Watcher);
        String data="测试数据";

        //zooKeeper.exists("/TEST02/TEST02-3",true);

        //注册子节点监视
        //List<String> children = zooKeeper.getChildren("/TEST02", true);
        //System.out.println(children);//返回现有的子节点
        //创建一个节点
        //String s = zooKeeper.create("/TEST02/TEST02-7", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);


        //返回创建的节点的路径
        //System.out.println(s);
        //zooKeeper.create("/TEST02/TEST02-4", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        //zooKeeper.setData("/TEST04","ABC".getBytes(),0);

        //获取节点的状态数据
        //Stat exists = zooKeeper.exists("/TEST", false);
        //System.out.println(exists);


        //Stat exists = zooKeeper.exists("/TEST02", true);
        //System.out.println("------->"+JSON.toJSONString(exists));
        //删除一个节点
        //zooKeeper.delete("/TEST02/TEST02-3",-1);

        //zooKeeper.close();

        List<String> children1 = zooKeeper.getChildren("/TEST02", true);
        System.out.println(children1);//返回现有的子节点

        Thread.sleep(50000);
    }

}
